package com.laizt.client.ftp;

import com.laizt.client.util.TestOffice;
import org.apache.ftpserver.ConnectionConfig;
import org.apache.ftpserver.ConnectionConfigFactory;
import org.apache.ftpserver.FtpServer;
import org.apache.ftpserver.FtpServerFactory;
import org.apache.ftpserver.ftplet.Authority;
import org.apache.ftpserver.impl.DefaultConnectionConfig;
import org.apache.ftpserver.listener.ListenerFactory;
import org.apache.ftpserver.usermanager.impl.BaseUser;
import org.apache.ftpserver.usermanager.impl.WritePermission;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

public class FtpMakerUtil {
    private static final Logger logger = LoggerFactory.getLogger(FtpMakerUtil.class);
    public static void makeFtpServer(){
        try{
            logger.info("ready start ftp!");
            FtpServerFactory serverFactory = new FtpServerFactory();
            ConnectionConfig connectionConfig = new DefaultConnectionConfig(true,500,Short.MAX_VALUE,10,3,0);
            serverFactory.setConnectionConfig(connectionConfig);

            ListenerFactory factory = new ListenerFactory();
            //设置监听端口
            factory.setPort(2121);
            //替换默认监听
            serverFactory.addListener("default", factory.createListener());
            //用户名
            BaseUser user = new BaseUser();
            user.setName("admin");
            //密码 如果不设置密码就是匿名用户
            user.setPassword("456");
            //用户主目录
            user.setHomeDirectory("E:\\ipc");
            List<Authority> authorities = new ArrayList<Authority>();
            //增加写权限
            authorities.add(new WritePermission());
            user.setAuthorities(authorities);
            //增加该用户
            serverFactory.getUserManager().save(user);

            FtpServer server = serverFactory.createServer();
            server.start();
            logger.info("ftp start success！===>account：admin，===>pwd：456");
        }catch (Exception e){
            e.printStackTrace();
            logger.info("开启ftp服务端失败！");
        }
    }
}
